package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private MenuDao menuDao;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = "%"+(queryPageBean.getQueryString()==null ? "" : queryPageBean.getQueryString())+"%";
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckItem> page = roleDao.findByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public List<Integer> findAssociation(Integer id) {
        List<Integer> perssionIds = roleDao.findAssociation(id);
        return perssionIds;
    }

    @Override
    public Role findById(Integer id) {
        Role role = roleDao.findById(id);
        return role;
    }

    //添加角色
    @Override
    public void addRole(Map map) {

        //数据类型转换
        List<Integer> menuIds = JSONObject.parseArray(map.get("menuIds").toString(),Integer.class);
        List<Integer> permissionIds = JSONObject.parseArray(map.get("permissionIds").toString(),Integer.class);
        Role role = JSONObject.parseObject(map.get("role").toString(),Role.class);

        //先插入t_role表数据
        roleDao.addRole(role);

        Integer roleId = role.getId();
        //设置role和menu之间的关联关系
        this.addMenuAssociation(roleId,menuIds);

        //设置role和permission之间的关联关系
        this.addPerAssociation(roleId,permissionIds);
    }

    @Override
    public List<Integer> findMenuAssociation(Integer id) {
        return roleDao.findMenuAssociation(id);
    }

    @Override
    public void updateRole(Map map) {
        //数据类型转换
        List<Integer> menuIds = JSONObject.parseArray(map.get("menuIds").toString(),Integer.class);
        List<Integer> permissionIds = JSONObject.parseArray(map.get("permissionIds").toString(),Integer.class);
        Role role = JSONObject.parseObject(map.get("role").toString(),Role.class);

        Integer roleId = role.getId();
        roleDao.deleteMenuAssociation(roleId);
        roleDao.deletePerAssociation(roleId);

        //更新与菜单的关联关系
        Map<String,Integer> mMap = new HashMap<>();
        for(Integer menuId : menuIds){
            mMap.put("role_id",roleId);
            mMap.put("menu_id",menuId);
            roleDao.addMenuAssociation(mMap);
        }

        //更新与权限的关联关系
        Map<String,Integer> pMap = new HashMap<>();
        for(Integer permissionId : permissionIds){
            pMap.put("role_id",roleId);
            pMap.put("permission_id",permissionId);
            roleDao.addPerAssociation(pMap);
        }

        //更新role数据
        roleDao.updateRole(role);
    }

    //删除角色
    @Override
    public void delete(Integer id) {
        //删除关联关系
        roleDao.deleteMenuAssociation(id);
        roleDao.deletePerAssociation(id);
        roleDao.deleteRole(id);
    }

    /**
     * 添加与菜单组的关联关系
     * @param roleId
     * @param menuIds
     */
    private void addMenuAssociation(Integer roleId,List<Integer> menuIds){
        Map<String,Integer> roleMenu = new HashMap<>();
        for(Integer menuId : menuIds){
            roleMenu.put("role_id",roleId);
            roleMenu.put("menu_id",menuId);
            roleDao.addMenuAssociation(roleMenu);
        }
    }

    /**
     * 添加与权限组的关联关系
     * @param roleId
     * @param permissionIds
     */
    private void addPerAssociation(Integer roleId,List<Integer> permissionIds){
        Map<String,Integer> roleP = new HashMap<>();
        for(Integer pId : permissionIds){
            roleP.put("role_id",roleId);
            roleP.put("permission_id",pId);
            roleDao.addPerAssociation(roleP);
        }
    }

}
